package org.flown.novel.data;

import com.baomidou.mybatisplus.annotation.*;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import lombok.Data;

import java.io.File;


@Data
@TableName("book")
public class Book
{
    public enum Column
    {
        ID("id"),
        COVER_ID("cover_id"),
        NAME("name"),
        AUTHOR_ID("author_id"),
        AUTHOR_NAME("author_name"),
        SYNOPSIS("synopsis"),
        CHAPTERS_ID("chaptersId"),
        PUBLISH_TIME("publish_time");

        private final String name;
        Column(String name){this.name = name;}
        public String getName(){return this.name;}
    }

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id = 0;
    private Integer coverId = 0;
    private String name;
    private Integer authorId = 0;
    private String authorName = "佚名";
    private String synopsis = "无力的简介";
    private String chaptersId = "[]";//用时候要转成json，[id1, id2, id3]

    @TableField(fill = FieldFill.INSERT, insertStrategy = FieldStrategy.IGNORED, updateStrategy = FieldStrategy.IGNORED)
    private java.util.Date publishTime;//自动填充

    private Integer clicks = 0;//点击量


    public Book(){}
    public Book(String name, String synopsis)
    {
        this.name = name;
        if (synopsis == null || synopsis.equals(""))
            return;
        this.synopsis = synopsis;
    }
    public Book(Integer author_id, String author_name, String name, String synopsis)
    {
        this.name = name;
        this.authorId = author_id;
        this.authorName = author_name;
        if (synopsis == null || synopsis.equals(""))
            return;
        this.synopsis = synopsis;
    }


    public String getCoverRealPath()
    {
        String base_path = DirectoryInformation.COVER.getRealPath() + "/" + this.coverId;
        File file = new File(base_path + ".jpg");
        if (file.exists())
            return base_path + ".jpg";

        file = new File(base_path + ".jpeg");
        if (file.exists())
            return base_path + ".jpeg";

        file = new File(base_path + ".png");
        if (file.exists())
            return base_path + ".png";

        file = new File(base_path + ".gif");
        if (file.exists())
            return base_path + ".gif";

        return DirectoryInformation.COVER.getRealPath() + "/0.jpg";
    }

    public void createDirectory()
    {
        String books_path = DirectoryInformation.BOOK.getRealPath() + "/" + this.id;
        File book_directory = new File(books_path);
        book_directory.mkdirs();//它会判断文件夹存在与否
    }

    public void deleteDirectory()
    {
        String books_path = DirectoryInformation.BOOK.getRealPath() + "/" + this.id;
        File book_directory = new File(books_path);
        book_directory.delete();//它会判断文件夹存在与否
    }


    public void addChapterID(int chapter_id)
            throws JsonProcessingException
    {
        ObjectMapper mapper = new ObjectMapper();
        ArrayNode node = (ArrayNode)mapper.readTree(this.getChaptersId());
        node.add(chapter_id);
        this.setChaptersId(node.toString());
    }
}


